/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : fTime.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Jan 13, 2009       Raghu Sarraff			       Created
 */
package net.lwf.handler.time.util;

import java.util.Calendar;
import java.util.StringTokenizer;

import net.lwf.handler.constant.IHandlerConstant;
import net.lwf.string.util.StringManipulator;



public class Time {
	
    private long seconds;
    private long minutes;
    private long hours;
    private long days;
    private long years;
    
	/**
	 * @param days
	 * @param hours
	 * @param minutes
	 * @param seconds
	 */
	public Time(long days, long hours, long minutes, long seconds) {
		this.days = days;
		this.hours = hours;
		this.minutes = minutes;
		this.seconds = seconds;
		this.reset();
	}
    /**
	 * @param hours
	 * @param minutes
	 * @param seconds
	 */
	public Time(long hours, long minutes, long seconds) {
		this.hours = hours;
		this.minutes = minutes;
		this.seconds = seconds;
		this.reset();
	}
	
	/**
	 * @return Returns the hours.
	 */
	public long getHours() {
		return hours;
	}
	
	/**
	 * @return Returns the minutes.
	 */
	public long getMinutes() {
		return minutes;
	}
	
	/**
	 * @return Returns the seconds.
	 */
	public long getSeconds() {
		return seconds;
	}
	
	
	
	/**
	 * @return Returns the days.
	 */
	public long getDays() {
		return days;
	}
	/**
	 * @return Returns the years.
	 */
	public long getYears() {
		return years;
	}
	//----------------------------------------------------------------------------------------------
	// Reset methods
	//----------------------------------------------------------------------------------------------

	
	/**
	 * Resets the time attributes if respective attribute value is greater than
	 * its limit (or standard) value.
	 */
	private void reset() {
		resetSeconds();
		resetMinutes();
		resetHours();
		resetDays();
	}
	
	/**
	 * Resets the seconds if the number of seconds is greater than 60.
	 * The resulting minutes are appropriately adjusted.
	 */
	private void resetSeconds() {
		long remainingsecs = this.seconds;
		while(remainingsecs - 60 >= 0) {
			this.minutes++;
			remainingsecs = remainingsecs - 60;
		}
		this.seconds = remainingsecs;
	}

	/**
	 * Resets the minutes if the number of minutes is greater than 60.
	 * The resulting hours are appropriately adjusted.
	 */
	private void resetMinutes() {
		long remainingmins = this.minutes;
		while(remainingmins - 60 >= 0) {
			this.hours++;
			remainingmins = remainingmins - 60;
		}
		this.minutes = remainingmins;
	}
	
	/**
	 * Resets the hours if the number of hours is greater than 24.
	 * The resulting days are appropriately adjusted.
	 */
	private void resetHours() {
		long remaininghrs = this.hours;
		while(remaininghrs - 24 >= 0) {
			this.days++;
			remaininghrs = remaininghrs - 24;
			//remaininghrs = remaininghrs - 12;   //Changed on 01/22/09 to display the formatted time
		}
		this.hours = remaininghrs;
	}
	
	/**
	 * Resets the dats if the number of days is greater than 365.
	 * The resulting years are appropriately adjusted.
	 */
	private void resetDays() {
		long remainingdays = this.days;
		while(remainingdays - 365 >= 0) {
			this.years++;
			remainingdays = remainingdays - 365;
		}
		this.days = remainingdays;
	}
	
	//----------------------------------------------------------------------------------------------
	// Utility methods 
	//----------------------------------------------------------------------------------------------
	
	/**
	 * Converts Calendar object with reference to the current date and 
	 * available time attributes (hours, minutes, seconds)
	 * @return
	 */
	public Calendar toCalendar() {
    	Calendar cal = Calendar.getInstance();
    	cal.set(Calendar.YEAR, Calendar.MONTH, Calendar.DATE, (int)this.hours, (int)this.minutes, (int)this.seconds);
    	return cal;
	}
	
	/**
	 * Gets the formatted time.
	 * 
	 * @return
	 */
	public String getFormattedTime() {
		return TimeUtil.getFormattedTime(this.hours, this.minutes, this.seconds);
	}
	
	public String getImagePath() {
		String imagepath = "/" + IHandlerConstant.CONTEXT_PATH + "/image/time/time_";
		String formattedtime = null;
		try {
			formattedtime = TimeUtil.getFormattedTime(this.hours, this.minutes, this.seconds);
			StringTokenizer stk = new StringTokenizer(formattedtime, " ");
			formattedtime = stk.nextToken();
			formattedtime = StringManipulator.replace(formattedtime, ":", "_");
			if(this.seconds == 0) {
				formattedtime = formattedtime + "_00";
			}
		}
		catch(Exception ex) {
			return "";
		}
		return imagepath + formattedtime + ".gif";
	}
	
	/**
	 * Adds additional time attribute values to the same existing values.
	 * Reset method is called to reset the time attributes due to the addition
	 * of time attributes.
	 * 
	 * @param hours    Hours
	 * @param minutes  Minutes
	 * @param seconds  Seconds
	 * 
	 */
	public void add(long hours, long minutes, long seconds) {
		this.hours += hours;
		this.minutes += minutes;
		this.seconds += seconds;
		reset();
	}

	public void print() {
		System.out.println("Input: Years: " + this.years + ", Days: " +  this.days + ", Hours: " + this.hours + ", Minutes: " + this.minutes + ", Seconds: " + this.seconds);
		System.out.println("Formatted time: " + getFormattedTime());
		System.out.println("Image Path: " + getImagePath());
	}

} //Time

